#include <queue>
#include <vector>
using namespace std;

struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode()
      : val(0), left(nullptr), right(nullptr){} * TreeNode(int x)
      : val(x),
        left(nullptr), right(nullptr) {}
  TreeNode(int x, TreeNode *left, TreeNode *right)
      : val(x), left(left), right(right) {}
};
vector<double> averageOfLevels(TreeNode *root) {
  if (root == nullptr)
    return {0};
  queue<TreeNode *> q;
  q.push(root);
  vector<double> res;
  while (!q.empty()) {
    int n = q.size();
    double sum = 0;
    int c = n;
    while (n--) {
      TreeNode *node = q.front();
      q.pop();
      sum += node->val;
      if (node->left)
        q.push(node->left);
      if (node->right)
        q.push(node->right);
    }
    res.push_back(sum / c);
  }
  return res;
}